/****************************************************************************

MIT License

Copyright (c) 2021 唐佐林
WeChat : delphi_tang
EMail: delphi_tang@dt4sw.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*****************************************************************************/


#include "gpio_impl.h"
#include <strings.h>

typedef struct 
{
    int type;
    mp_obj_t func;
    mp_obj_t arg;
} mp_custom_cb_t;

typedef struct
{
    const int gpio;
    const char* func;
    const int value;
} gpio_func_val_t;

static volatile mp_custom_cb_t g_callback[WIFI_IOT_GPIO_IDX_MAX] = {0};

static gpio_func_val_t g_func_value[] = 
{
    {0, "GPIO", WIFI_IOT_IO_FUNC_GPIO_0_GPIO},
    {0, "UART1_TXD", WIFI_IOT_IO_FUNC_GPIO_0_UART1_TXD},
    {0, "SPI1_CK", WIFI_IOT_IO_FUNC_GPIO_0_SPI1_CK},
    {0, "JTAG_TDO", WIFI_IOT_IO_FUNC_GPIO_0_JTAG_TDO},
    {0, "PWM3_OUT", WIFI_IOT_IO_FUNC_GPIO_0_PWM3_OUT},
    {0, "I2C1_SDA", WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA},
    {1, "GPIO", WIFI_IOT_IO_FUNC_GPIO_1_GPIO},
    {1, "UART1_RXD", WIFI_IOT_IO_FUNC_GPIO_1_UART1_RXD},
    {1, "SPI1_RXD", WIFI_IOT_IO_FUNC_GPIO_1_SPI1_RXD},
    {1, "JTAG_TCK", WIFI_IOT_IO_FUNC_GPIO_1_JTAG_TCK},
    {1, "PWM4_OUT", WIFI_IOT_IO_FUNC_GPIO_1_PWM4_OUT},
    {1, "I2C1_SCL", WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL},
    {1, "BT_FREQ", WIFI_IOT_IO_FUNC_GPIO_1_BT_FREQ},
    {2, "GPIO", WIFI_IOT_IO_FUNC_GPIO_2_GPIO},
    {2, "UART1_RTS_N", WIFI_IOT_IO_FUNC_GPIO_2_UART1_RTS_N},
    {2, "SPI1_TXD", WIFI_IOT_IO_FUNC_GPIO_2_SPI1_TXD},
    {2, "JTAG_TRSTN", WIFI_IOT_IO_FUNC_GPIO_2_JTAG_TRSTN},
    {2, "PWM2_OUT", WIFI_IOT_IO_FUNC_GPIO_2_PWM2_OUT},
    {2, "SSI_CLK", WIFI_IOT_IO_FUNC_GPIO_2_SSI_CLK},
    {3, "GPIO", WIFI_IOT_IO_FUNC_GPIO_3_GPIO},
    {3, "UART0_TXD", WIFI_IOT_IO_FUNC_GPIO_3_UART0_TXD},
    {3, "UART1_CTS_N", WIFI_IOT_IO_FUNC_GPIO_3_UART1_CTS_N},
    {3, "SPI1_CSN", WIFI_IOT_IO_FUNC_GPIO_3_SPI1_CSN},
    {3, "JTAG_TDI", WIFI_IOT_IO_FUNC_GPIO_3_JTAG_TDI},
    {3, "PWM5_OUT", WIFI_IOT_IO_FUNC_GPIO_3_PWM5_OUT},
    {3, "I2C1_SDA", WIFI_IOT_IO_FUNC_GPIO_3_I2C1_SDA},
    {3, "SSI_DATA", WIFI_IOT_IO_FUNC_GPIO_3_SSI_DATA},
    {4, "GPIO", WIFI_IOT_IO_FUNC_GPIO_4_GPIO},
    {4, "UART0_RXD", WIFI_IOT_IO_FUNC_GPIO_4_UART0_RXD},
    {4, "JTAG_TMS", WIFI_IOT_IO_FUNC_GPIO_4_JTAG_TMS},
    {4, "PWM1_OUT", WIFI_IOT_IO_FUNC_GPIO_4_PWM1_OUT},
    {4, "I2C1_SCL", WIFI_IOT_IO_FUNC_GPIO_4_I2C1_SCL},
    {5, "GPIO", WIFI_IOT_IO_FUNC_GPIO_5_GPIO},
    {5, "UART1_RXD", WIFI_IOT_IO_FUNC_GPIO_5_UART1_RXD},
    {5, "SPI0_CSN", WIFI_IOT_IO_FUNC_GPIO_5_SPI0_CSN},
    {5, "PWM2_OUT", WIFI_IOT_IO_FUNC_GPIO_5_PWM2_OUT},
    {5, "I2S0_MCLK", WIFI_IOT_IO_FUNC_GPIO_5_I2S0_MCLK},
    {5, "BT_STATUS", WIFI_IOT_IO_FUNC_GPIO_5_BT_STATUS},
    {6, "GPIO", WIFI_IOT_IO_FUNC_GPIO_6_GPIO},
    {6, "UART1_TXD", WIFI_IOT_IO_FUNC_GPIO_6_UART1_TXD},
    {6, "SPI0_CK", WIFI_IOT_IO_FUNC_GPIO_6_SPI0_CK},
    {6, "PWM3_OUT", WIFI_IOT_IO_FUNC_GPIO_6_PWM3_OUT},
    {6, "I2S0_TX", WIFI_IOT_IO_FUNC_GPIO_6_I2S0_TX},
    {6, "COEX_SWITCH", WIFI_IOT_IO_FUNC_GPIO_6_COEX_SWITCH},
    {7, "GPIO", WIFI_IOT_IO_FUNC_GPIO_7_GPIO},
    {7, "UART1_CTS_N", WIFI_IOT_IO_FUNC_GPIO_7_UART1_CTS_N},
    {7, "SPI0_RXD", WIFI_IOT_IO_FUNC_GPIO_7_SPI0_RXD},
    {7, "PWM0_OUT", WIFI_IOT_IO_FUNC_GPIO_7_PWM0_OUT},
    {7, "I2S0_BCLK", WIFI_IOT_IO_FUNC_GPIO_7_I2S0_BCLK},
    {7, "BT_ACTIVE", WIFI_IOT_IO_FUNC_GPIO_7_BT_ACTIVE},
    {8, "GPIO", WIFI_IOT_IO_FUNC_GPIO_8_GPIO},
    {8, "UART1_RTS_N", WIFI_IOT_IO_FUNC_GPIO_8_UART1_RTS_N},
    {8, "SPI0_TXD", WIFI_IOT_IO_FUNC_GPIO_8_SPI0_TXD},
    {8, "PWM1_OUT", WIFI_IOT_IO_FUNC_GPIO_8_PWM1_OUT},
    {8, "I2S0_WS", WIFI_IOT_IO_FUNC_GPIO_8_I2S0_WS},
    {8, "WLAN_ACTIVE", WIFI_IOT_IO_FUNC_GPIO_8_WLAN_ACTIVE},
    {9, "GPIO", WIFI_IOT_IO_FUNC_GPIO_9_GPIO},
    {9, "I2C0_SCL", WIFI_IOT_IO_FUNC_GPIO_9_I2C0_SCL},
    {9, "UART2_RTS_N", WIFI_IOT_IO_FUNC_GPIO_9_UART2_RTS_N},
    {9, "SDIO_D2", WIFI_IOT_IO_FUNC_GPIO_9_SDIO_D2},
    {9, "SPI0_TXD", WIFI_IOT_IO_FUNC_GPIO_9_SPI0_TXD},
    {9, "PWM0_OUT", WIFI_IOT_IO_FUNC_GPIO_9_PWM0_OUT},
    {9, "I2S0_MCLK", WIFI_IOT_IO_FUNC_GPIO_9_I2S0_MCLK},
    {10, "GPIO", WIFI_IOT_IO_FUNC_GPIO_10_GPIO},
    {10, "I2C0_SDA", WIFI_IOT_IO_FUNC_GPIO_10_I2C0_SDA},
    {10, "UART2_CTS_N", WIFI_IOT_IO_FUNC_GPIO_10_UART2_CTS_N},
    {10, "SDIO_D3", WIFI_IOT_IO_FUNC_GPIO_10_SDIO_D3},
    {10, "SPI0_CK", WIFI_IOT_IO_FUNC_GPIO_10_SPI0_CK},
    {10, "PWM1_OUT", WIFI_IOT_IO_FUNC_GPIO_10_PWM1_OUT},
    {10, "I2S0_TX", WIFI_IOT_IO_FUNC_GPIO_10_I2S0_TX},
    {11, "GPIO", WIFI_IOT_IO_FUNC_GPIO_11_GPIO},
    {11, "UART2_TXD", WIFI_IOT_IO_FUNC_GPIO_11_UART2_TXD},
    {11, "SDIO_CMD", WIFI_IOT_IO_FUNC_GPIO_11_SDIO_CMD},
    {11, "SPI0_RXD", WIFI_IOT_IO_FUNC_GPIO_11_SPI0_RXD},
    {11, "PWM2_OUT", WIFI_IOT_IO_FUNC_GPIO_11_PWM2_OUT},
    {11, "RF_TX_EN_EXT", WIFI_IOT_IO_FUNC_GPIO_11_RF_TX_EN_EXT},
    {11, "I2S0_RX", WIFI_IOT_IO_FUNC_GPIO_11_I2S0_RX},
    {12, "GPIO", WIFI_IOT_IO_FUNC_GPIO_12_GPIO},
    {12, "UART2_RXD", WIFI_IOT_IO_FUNC_GPIO_12_UART2_RXD},
    {12, "SDIO_CLK", WIFI_IOT_IO_FUNC_GPIO_12_SDIO_CLK},
    {12, "SPI0_CSN", WIFI_IOT_IO_FUNC_GPIO_12_SPI0_CSN},
    {12, "PWM3_OUT", WIFI_IOT_IO_FUNC_GPIO_12_PWM3_OUT},
    {12, "RF_RX_EN_EXT", WIFI_IOT_IO_FUNC_GPIO_12_RF_RX_EN_EXT},
    {12, "I2S0_BCLK", WIFI_IOT_IO_FUNC_GPIO_12_I2S0_BCLK},
    {13, "SSI_DATA", WIFI_IOT_IO_FUNC_GPIO_13_SSI_DATA},
    {13, "UART0_TXD", WIFI_IOT_IO_FUNC_GPIO_13_UART0_TXD},
    {13, "UART2_RTS_N", WIFI_IOT_IO_FUNC_GPIO_13_UART2_RTS_N},
    {13, "SDIO_D0", WIFI_IOT_IO_FUNC_GPIO_13_SDIO_D0},
    {13, "GPIO", WIFI_IOT_IO_FUNC_GPIO_13_GPIO},
    {13, "PWM4_OUT", WIFI_IOT_IO_FUNC_GPIO_13_PWM4_OUT},
    {13, "I2C0_SDA", WIFI_IOT_IO_FUNC_GPIO_13_I2C0_SDA},
    {13, "I2S0_WS", WIFI_IOT_IO_FUNC_GPIO_13_I2S0_WS},
    {14, "SSI_CLK", WIFI_IOT_IO_FUNC_GPIO_14_SSI_CLK},
    {14, "UART0_RXD", WIFI_IOT_IO_FUNC_GPIO_14_UART0_RXD},
    {14, "UART2_CTS_N", WIFI_IOT_IO_FUNC_GPIO_14_UART2_CTS_N},
    {14, "SDIO_D1", WIFI_IOT_IO_FUNC_GPIO_14_SDIO_D1},
    {14, "GPIO", WIFI_IOT_IO_FUNC_GPIO_14_GPIO},
    {14, "PWM5_OUT", WIFI_IOT_IO_FUNC_GPIO_14_PWM5_OUT},
    {14, "I2C0_SCL", WIFI_IOT_IO_FUNC_GPIO_14_I2C0_SCL},
};

static void gpio_callback_func(char* data)
{
    int index = (size_t)data;
    
    if( g_callback[index].type )
    {
        mp_call_function_1(g_callback[index].func, g_callback[index].arg);
    }
}

mp_obj_t mp_gpio_init()
{
    return mp_obj_new_int(GpioInit());
}

mp_obj_t mp_gpio_deinit()
{
    return mp_obj_new_int(GpioDeinit());
}

mp_obj_t mp_gpio_get_dir(mp_obj_t idx)
{
    int index = mp_obj_get_int(idx);
    WifiIotGpioDir dir = 0;
    unsigned int r = GpioGetDir((WifiIotGpioIdx)index, &dir);
    mp_obj_t ret[2] = 
    {
        mp_obj_new_int(r),
        mp_obj_new_int(dir),
    };
    
    return mp_obj_new_tuple(2, ret);
}

mp_obj_t mp_gpio_set_dir(mp_obj_t idx, mp_obj_t dir)
{
    int index = mp_obj_get_int(idx);
    int direction = mp_obj_get_int(dir);
    unsigned int ret = GpioSetDir((WifiIotGpioIdx)index, (WifiIotGpioDir)direction);
    
    return mp_obj_new_int(ret);
}

mp_obj_t mp_gpio_get_output(mp_obj_t idx)
{
    int index = mp_obj_get_int(idx);
    WifiIotGpioValue val = 0;
    unsigned int r = GpioGetOutputVal((WifiIotGpioIdx)index, &val);
    mp_obj_t ret[2] = 
    {
        mp_obj_new_int(r),
        mp_obj_new_int(val),
    };
    
    return mp_obj_new_tuple(2, ret);
}

mp_obj_t mp_gpio_set_output(mp_obj_t idx, mp_obj_t val)
{
    int index = mp_obj_get_int(idx);
    int value = !!mp_obj_get_int(val);
    unsigned int ret = GpioSetOutputVal((WifiIotGpioIdx)index, (WifiIotGpioValue)value);
    
    return mp_obj_new_int(ret);
}

mp_obj_t mp_gpio_get_input(mp_obj_t idx)
{
    int index = mp_obj_get_int(idx);
    WifiIotGpioValue val = 0;
    unsigned int r = GpioGetInputVal((WifiIotGpioIdx)index, &val);
    mp_obj_t ret[2] = 
    {
        mp_obj_new_int(r),
        mp_obj_new_int(val),
    };
    
    return mp_obj_new_tuple(2, ret);
}

mp_obj_t mp_gpio_set_isr_mode(mp_obj_t idx, mp_obj_t mode)
{
    int index = mp_obj_get_int(idx);
    int type = mp_obj_get_int(mode);
    int r = -1;
    
    if( (0 <= index) && (index < WIFI_IOT_GPIO_IDX_MAX) && (0 < type) && (type < GPIO_TYPE_MAX) )
    {
        WifiIotGpioIntType itype = 0;
        WifiIotGpioIntPolarity ipolarity = 0;
        
        switch( type )
        {
            case GPIO_TYPE_LEVEL_LOW:
                itype = WIFI_IOT_INT_TYPE_LEVEL;
                ipolarity = WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW;
                break;
            case GPIO_TYPE_LEVEL_HIGH:
                itype = WIFI_IOT_INT_TYPE_LEVEL;
                ipolarity = WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH;
                break;
            case GPIO_TYPE_EDGE_FALL_LOW:
                itype = WIFI_IOT_INT_TYPE_EDGE;
                ipolarity = WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW;
                break;
            case GPIO_TYPE_EDGE_RISE_HIGH:
                itype = WIFI_IOT_INT_TYPE_EDGE;
                ipolarity = WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH;
                break;    
        }
        
        if( (r = GpioSetIsrMode((WifiIotGpioIdx)index, itype, ipolarity)) == 0 )
        {
            g_callback[index].type = type;
        }
    }
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_gpio_get_isr_mode(mp_obj_t idx)
{
    int index = mp_obj_get_int(idx);
    int ret = -1;
    
    if( (0 <= index) && (index < WIFI_IOT_GPIO_IDX_MAX) )
    {
        ret = g_callback[index].type;
    }
    
    return mp_obj_new_int(ret);
}

mp_obj_t mp_gpio_set_isr_mask(mp_obj_t idx, mp_obj_t mask)
{
    int index = mp_obj_get_int(idx);
    unsigned char msk = (unsigned char)mp_obj_get_int(mask);
    int r = -1;
    
    if( (0 <= index) && (index < WIFI_IOT_GPIO_IDX_MAX) )
    {
        r = GpioSetIsrMask((WifiIotGpioIdx)index, msk);
    }
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_gpio_register_isr_func(mp_obj_t idx, mp_obj_t func, mp_obj_t arg)
{
    int index = mp_obj_get_int(idx);
    unsigned int r = -1;
    
    if( (0 <= index) && (index < WIFI_IOT_GPIO_IDX_MAX) && g_callback[index].type )
    {
        WifiIotGpioIntType itype = 0;
        WifiIotGpioIntPolarity ipolarity = 0;
        
        g_callback[index].func = func;
        g_callback[index].arg = arg;
        
        switch( g_callback[index].type )
        {
            case GPIO_TYPE_LEVEL_LOW:
                itype = WIFI_IOT_INT_TYPE_LEVEL;
                ipolarity = WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW;
                break;
            case GPIO_TYPE_LEVEL_HIGH:
                itype = WIFI_IOT_INT_TYPE_LEVEL;
                ipolarity = WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH;
                break;
            case GPIO_TYPE_EDGE_FALL_LOW:
                itype = WIFI_IOT_INT_TYPE_EDGE;
                ipolarity = WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW;
                break;
            case GPIO_TYPE_EDGE_RISE_HIGH:
                itype = WIFI_IOT_INT_TYPE_EDGE;
                ipolarity = WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH;
                break;    
        }
        
        r = GpioRegisterIsrFunc((WifiIotGpioIdx)index, itype, ipolarity, gpio_callback_func, (char*)(size_t)index);
    }
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_gpio_unregister_isr_func(mp_obj_t idx)
{
    int index = mp_obj_get_int(idx);
    unsigned int r = -1;
    
    if( (0 <= index) && (index < WIFI_IOT_GPIO_IDX_MAX) )
    {
        g_callback[index].type = 0;
        
        r = GpioUnregisterIsrFunc((WifiIotGpioIdx)index);
    }
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_gpio_set_pull(mp_obj_t idx, mp_obj_t val)
{
    int index = mp_obj_get_int(idx);
    int value = mp_obj_get_int(val);
    unsigned int r = -1;
    
    if( (WIFI_IOT_IO_PULL_NONE <= value) && (value <= WIFI_IOT_IO_PULL_MAX) )
    {
        r = IoSetPull((WifiIotIoName)index, (WifiIotIoPull)value);
    }
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_gpio_get_pull(mp_obj_t idx)
{
    int index = mp_obj_get_int(idx);
    WifiIotIoPull val = 0;
    unsigned int r = IoGetPull((WifiIotIoName)index, &val);
    mp_obj_t ret[2] = 
    {
        mp_obj_new_int(r),
        mp_obj_new_int(val),
    };
    
    return mp_obj_new_tuple(2, ret);
}

mp_obj_t mp_gpio_set_func(mp_obj_t idx, mp_obj_t val)
{
    int index = mp_obj_get_int(idx);
    int value = mp_obj_get_int(val);
    unsigned int ret = IoSetFunc((WifiIotIoName)index, (unsigned char)value);
    
    return mp_obj_new_int(ret);
}

mp_obj_t mp_gpio_get_func(mp_obj_t idx)
{
    int index = mp_obj_get_int(idx);
    unsigned char val = 0;
    unsigned int r = IoGetFunc((WifiIotIoName)index, &val);
    mp_obj_t ret[2] = 
    {
        mp_obj_new_int(r),
        mp_obj_new_int(val),
    };
    
    return mp_obj_new_tuple(2, ret);
}

mp_obj_t mp_gpio_set_drv_strength(mp_obj_t idx, mp_obj_t val)
{
    int index = mp_obj_get_int(idx);
    int value = mp_obj_get_int(val);
    unsigned int r = -1;
    
    if( (0 <= value) && (value < WIFI_IOT_IO_DRIVER_STRENGTH_MAX) )
    {
        r = IOSetDriverStrength((WifiIotIoName)index, (WifiIotIoDriverStrength)value);
    }
    
    return mp_obj_new_int(r);
}

mp_obj_t mp_gpio_get_drv_strength(mp_obj_t idx)
{
    int index = mp_obj_get_int(idx);
    WifiIotIoDriverStrength val = 0;
    unsigned int r = IOGetDriverStrength((WifiIotIoName)index, &val);
    mp_obj_t ret[2] = 
    {
        mp_obj_new_int(r),
        mp_obj_new_int(val),
    };
    
    return mp_obj_new_tuple(2, ret);
}

mp_obj_t mp_gpio_query_func_value(mp_obj_t idx, mp_obj_t key)
{
    mp_obj_t ret = mp_obj_new_int(-1);
    int index = mp_obj_get_int(idx);
    const char* func = mp_obj_str_get_str(key);
    int len = sizeof(g_func_value)/sizeof(*g_func_value);
    int i = 0;
    
    for(i=0; i<len; i++)
    {
        if( (index == g_func_value[i].gpio) && (strcasecmp(func, g_func_value[i].func) == 0) )
        {
            ret = mp_obj_new_int(g_func_value[i].value);
            break;
        }
    }
    
    return ret;
}
